// 游戏配置
import { reactive } from 'vue';
import { DeckConfigData, GameId } from '/@/api/currentDeck/model/deckModel';
import { awaitWrap } from '/@/utils';
import { DeckConfigApi } from '/@/api/currentDeck/deck';
import { useCurrentDeckStoreWithOut } from '/@/store/modules/currentDeck';
import { isArray } from '/@/utils/is';

const { setCardGroup, setDeckConfig, clearCreateDeckParams } = useCurrentDeckStoreWithOut();

const deckConfigData = reactive<DeckConfigData>({} as DeckConfigData);

const getDeckConfig = (params: GameId) => {
  const deckConfig = async () => {
    const [error, result] = await awaitWrap(DeckConfigApi({ game_id: params.game_id }));
    if (error || !result) {
      return;
    }
    for (const item in result) {
      deckConfigData[item] = result[item];
    }
    deckConfigData.tags = isArray(result.tags) ? result.tags : [];
    setDeckConfig(deckConfigData);
    setCardGroup(deckConfigData.card_group);
    clearCreateDeckParams();
  };
  deckConfig();
};

export const useCurrentDeckConfigHooks = (params: GameId) => {
  getDeckConfig(params);
  return {
    deckConfigData,
  };
};
